package org.csu.mypetstoreapi.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.mypetstoreapi.common.CommonResponse;
import org.csu.mypetstoreapi.entity.Category;
import org.csu.mypetstoreapi.entity.Item;
import org.csu.mypetstoreapi.entity.Product;
import org.csu.mypetstoreapi.persistence.CategoryMapper;
import org.csu.mypetstoreapi.persistence.ItemMapper;
import org.csu.mypetstoreapi.persistence.ProductMapper;
import org.csu.mypetstoreapi.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("CatalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public CommonResponse<Category> findCategoryById(String catid) {
        Category category=  categoryMapper.selectById(catid);
        if(category==null){
            return CommonResponse.createForSuccessMessage("没有该ID的Category");
        }
        return CommonResponse.createForSuccess(category);
    }

    @Override
    public CommonResponse<List<Product>> searchProductList(String keyword) {
        QueryWrapper<Product> searchProductQueryWrapper = new QueryWrapper<>();
        searchProductQueryWrapper.like("name",keyword);

        List<Product> searchProductList = productMapper.selectList(searchProductQueryWrapper);
        if(searchProductList.isEmpty()){
            return CommonResponse.createForSuccessMessage("没有相关关键字的product");

        }
        return CommonResponse.createForSuccess(searchProductList);
    }

    @Override
    public CommonResponse<List<Category>> findAllCategorys() {
       List<Category> categoryList=categoryMapper.selectList(null);
       if(categoryList.isEmpty()){
           return CommonResponse.createForSuccessMessage("没有分类信息");

       }
       return CommonResponse.createForSuccess(categoryList);
    }

    @Override
    public CommonResponse<Product> findProductByProductId(String productid) {
        Product product=productMapper.selectById(productid);
        if(product==null){
            return  CommonResponse.createForSuccessMessage("没有该ID的Product");
        }
        return CommonResponse.createForSuccess(product);
    }

    @Override
    public CommonResponse<List<Product>> findProductByCategory(String category) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category", category);
        List<Product> productList = productMapper.selectList(queryWrapper);
        if (productList.isEmpty()) {
            return CommonResponse.createForSuccessMessage("没有该分类下的Product");
        }
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<List<Product>> findAllProducts() {
        List<Product> productList = productMapper.selectList(null);
        if (productList.isEmpty()) {
            return CommonResponse.createForSuccessMessage("没有Product信息");
        }
        return CommonResponse.createForSuccess(productList);
    }

    @Override
    public CommonResponse<Item> findItemById(String itemid) {
        Item item = itemMapper.selectById(itemid);
        if (item == null) {
            return CommonResponse.createForSuccessMessage("没有该ID的Item");
        }
        return CommonResponse.createForSuccess(item);
    }

    @Override
    public CommonResponse<List<Item>> findItemByProductId(String productid) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid", productid);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if (itemList.isEmpty()) {
            return CommonResponse.createForSuccessMessage("没有该Product ID下的Item");
        }
        return CommonResponse.createForSuccess(itemList);
    }

    @Override
    public CommonResponse<List<Item>> findItemByName(String productName) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", productName);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if (itemList.isEmpty()) {
            return CommonResponse.createForSuccessMessage("没有该名称的Item");
        }
        return CommonResponse.createForSuccess(itemList);
    }

    @Override
    public CommonResponse<List<Item>> findItemByCategory(String categoryId) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", categoryId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        if (itemList.isEmpty()) {
            return CommonResponse.createForSuccessMessage("没有该分类下的Item");
        }
        return CommonResponse.createForSuccess(itemList);
    }

    @Override
    public CommonResponse<List<Item>> allItems() {
        List<Item> itemList = itemMapper.selectList(null);
        if (itemList.isEmpty()) {
            return CommonResponse.createForSuccessMessage("没有Item信息");
        }
        return CommonResponse.createForSuccess(itemList);
    }
}
